package com.crazy.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author panyonghui
 * @describe Excel工具类
 * @date 2022/9/14 16:13
 */
public class ExcelUtils {
    private static Log log = LogFactory.getLog(ExcelUtils.class);

    /**
     * @describe excel读取
     * @auther crazy_cat
     * @date 2022/9/14 17:10
     * @param filePath
     * @return java.util.List
     * @modify
     */
    public static List excelRead(String filePath, Map keyMap) {
        // 用于接收数据
        List<Map<Integer,String>> list = new LinkedList<>();
        AnalysisEventListener analysisEventListener = new AnalysisEventListener<Map<Integer,String>>() {
            // 解析完每一行数据后执行
            @Override
            public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) {
                Map dataMap = new HashMap();
                integerStringMap.forEach((k, v) ->
                    dataMap.put(keyMap.get(k), v)
                );
                list.add(dataMap);
            }

            // 读取完全部数据后执行
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("数据读取完毕：" + list);
            }
        };
        // excel读取
        EasyExcel.read(filePath).sheet().registerReadListener(analysisEventListener).doRead();
        return list;
    }

    public static void excelWrite(String filePath, List dataList){
    }

    public static void main(String[] args) {
        Map<Integer, String> keyMap = new HashMap();
        keyMap.put(0, "prdSeq");
        keyMap.put(1, "prdName");
        keyMap.put(2, "prdCode");
        keyMap.put(3, "prdState");
        String filePath = "C:\\Users\\crazycat\\Desktop\\可续存产品清单(2).xls";
        List<Map> list = excelRead(filePath, keyMap);
        System.out.println(JSONObject.toJSONString(list));

        List resList1 = list.stream().filter(map -> !"已包装".equals(map.get("prdState"))).collect(Collectors.toList());
        System.out.println("resList1:" + resList1);

        List resList2 = list.parallelStream().filter(map -> !"已包装".equals(map.get("prdState"))).collect(Collectors.toList());
        System.out.println("resList2:" + resList2);

        String prdNames1 = list.stream().map(map -> (String)map.get("prdName")).collect(Collectors.joining(","));
        System.out.println("prdNames1:" + prdNames1);

        List resList3 = list.stream().filter(map -> !"已包装".equals(map.get("prdState"))).peek(map -> {
            System.out.println(map);
        }).limit(3).collect(Collectors.toList());
        System.out.println("resList3:" + resList3);

        List resList4 = list.parallelStream().filter(map -> !"已包装".equals(map.get("prdState"))).map(map -> {
            System.out.println(map.get("prdName"));
            return map.get("prdName");
        }).collect(Collectors.toList());

        System.out.println("resList4:" + resList4);
    }
}
